//
//  PNestController.swift
//  CJPack
//
//  Created by 陈警卫 on 2023-05-30.
//

import UIKit

class PNestController: CJBaseMenuController {

    var tableView: UITableView!
    var datas: [[String : String]] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupView()
        setupData()
    }
    
    func setupView() {
        
        self.tableView = UITableView(frame: CGRect(x: 0, 
                                                   y: CJScreen.navBatBarHeight,
                                                   width: self.view.cj_width,
                                                   height: self.view.cj_height - CJScreen.navBatBarHeight), 
                                     style: .plain)
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.separatorStyle = .none
        self.view.addSubview(self.tableView)
        
        self.tableView.register(PNestCell.self, forCellReuseIdentifier: "PNestCell")
    }
    
    func setupData() {
        
        datas = [
            ["name": "Charts",   "vc": "PChartsController",   "type": "swift"],
            ["name": "OCCharts", "vc": "OCPChartsController", "type": "oc"],
        ]
        self.tableView.reloadData()
    }

}

extension PNestController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PNestCell", for: indexPath)
        cell.selectionStyle = .none
        
        if let cell = cell as? PNestCell {
            cell.param = self.datas[indexPath.item]
        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let param = self.datas[indexPath.item]
        selectAction(param: param)
    }
}

extension PNestController {
    
    func selectAction(param: [String : String]) {
        
        seletPushAction(param: param)
    }
    
    func seletPushAction(param: [String : String]) {
        guard let vcStr = param["vc"] else { return }
        let isOC = param["type"] == "oc"
        guard let vcClass = isOC ? vcStr.cj_class_oc() : vcStr.cj_class() else { return  }
        guard let vcType = vcClass as? UIViewController.Type else { return }
        let vc = vcType.init()
        navigationController?.pushViewController(vc, animated: true)
    }
    
}




class PNestCell: UITableViewCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.setupView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var backV: UIView!
    var titleL: UILabel!
    
    var param: [String: String]? {
        didSet {
            self.titleL.text = param?["name"] ?? ""
        }
    }
    
    func setupView() {
        
        self.backV = UIView()
        self.addSubview(self.backV)
        
        self.titleL = UILabel()
        self.backV.addSubview(self.titleL)
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.backV.frame = CGRect(x: 0, y: 0, width: self.cj_width, height: self.cj_height)
        self.titleL.frame = CGRect(x: 20, y: 0, width: self.backV.cj_width - 40, height: 30)
        self.titleL.cj_centerY = self.backV.cj_height * 0.5
    }
    
}

